/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * productEntryForm.java
 *
 * Created on Dec 11, 2011, 9:38:25 PM
 */
package svp;

import java.awt.Color;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;
import org.jdesktop.application.Task;
import svp.exceptions.IllegalOrphanException;
import svp.exceptions.NonexistentEntityException;
import svp.exceptions.PreexistingEntityException;

/**
 *
 * @author kandamaran
 */
public class ProductEntryForm extends javax.swing.JDialog implements FocusListener {

    /** Creates new form productEntryForm */
    public ProductEntryForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(svp.SVPApp.class).getContext().getResourceMap(ProductEntryForm.class);
        SVPPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory(resourceMap.getString("SVPPUEntityManager.persistenceUnit")).createEntityManager(); // NOI18N
        jLabel1 = new javax.swing.JLabel();
        productNameCB = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        companyNameCB = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        colorCB = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        unitCB = new javax.swing.JComboBox();
        addColorBT = new javax.swing.JButton();
        deleteColorBT = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        mrpTF = new javax.swing.JTextField();
        qtyTF = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        productNameCB.setEditable(true);
        productNameCB.setFont(resourceMap.getFont("productNameCB.font")); // NOI18N
        //List productNameList = SVPPUEntityManager.createNamedQuery("ProductTbl.name").getResultList();
        //Collections.sort(productNameList);
        productNameCB.setModel(new javax.swing.DefaultComboBoxModel ());
        //new S17NonStrict(productNameCB);
        //JTextField productNameTF = (JTextField)productNameCB.getEditor().getEditorComponent();
        //productNameTF.setText("");
        productNameCB.setName("productNameCB"); // NOI18N
        productNameCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                productNameCBActionPerformed(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        companyNameCB.setEditable(true);
        companyNameCB.setFont(resourceMap.getFont("companyNameCB.font")); // NOI18N
        List companyNameList = SVPPUEntityManager.createNamedQuery("CompanyTbl.name").getResultList();
        Collections.sort(companyNameList);
        companyNameCB.setModel(new javax.swing.DefaultComboBoxModel(companyNameList.toArray()));
        new S17NonStrict(companyNameCB);
        JTextField companyNameTF = (JTextField)companyNameCB.getEditor().getEditorComponent();
        companyNameTF.setText("");
        companyNameCB.setName("companyNameCB"); // NOI18N
        companyNameCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                companyNameCBActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel10.setFont(resourceMap.getFont("jLabel10.font")); // NOI18N
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel10)
                .addContainerGap(468, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jLabel6)
                .add(jLabel10))
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(svp.SVPApp.class).getContext().getActionMap(ProductEntryForm.class, this);
        jButton4.setAction(actionMap.get("showOptionPane")); // NOI18N
        jButton4.setFont(resourceMap.getFont("jButton4.font")); // NOI18N
        jButton4.setIcon(resourceMap.getIcon("jButton4.icon")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        jLabel12.setFont(resourceMap.getFont("jLabel12.font")); // NOI18N
        jLabel12.setForeground(resourceMap.getColor("jLabel12.foreground")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel13.setFont(resourceMap.getFont("jLabel13.font")); // NOI18N
        jLabel13.setForeground(resourceMap.getColor("jLabel13.foreground")); // NOI18N
        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        jLabel16.setFont(resourceMap.getFont("jLabel16.font")); // NOI18N
        jLabel16.setForeground(resourceMap.getColor("jLabel16.foreground")); // NOI18N
        jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jButton5.setIcon(resourceMap.getIcon("jButton5.icon")); // NOI18N
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton5.setName("jButton5"); // NOI18N
        jButton5.setOpaque(false);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        colorCB.setEditable(true);
        colorCB.setFont(resourceMap.getFont("colorCB.font")); // NOI18N
        colorCB.setName("colorCB"); // NOI18N
        colorCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorCBActionPerformed(evt);
            }
        });
        colorCB.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                colorCBFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                colorCBFocusLost(evt);
            }
        });
        colorCB.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                colorCBKeyPressed(evt);
            }
        });

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        unitCB.setEditable(true);
        unitCB.setFont(resourceMap.getFont("unitCB.font")); // NOI18N
        unitCB.setName("unitCB"); // NOI18N
        unitCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unitCBActionPerformed(evt);
            }
        });

        addColorBT.setIcon(resourceMap.getIcon("addColorBT.icon")); // NOI18N
        addColorBT.setText(resourceMap.getString("addColorBT.text")); // NOI18N
        addColorBT.setEnabled(false);
        addColorBT.setName("addColorBT"); // NOI18N
        addColorBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addColorBTActionPerformed(evt);
            }
        });

        deleteColorBT.setAction(actionMap.get("deleteProductColor")); // NOI18N
        deleteColorBT.setIcon(resourceMap.getIcon("deleteColorBT.icon")); // NOI18N
        deleteColorBT.setText(resourceMap.getString("deleteColorBT.text")); // NOI18N
        deleteColorBT.setName("deleteColorBT"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel14.setFont(resourceMap.getFont("jLabel14.font")); // NOI18N
        jLabel14.setForeground(resourceMap.getColor("jLabel14.foreground")); // NOI18N
        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        mrpTF.setFont(resourceMap.getFont("mrpTF.font")); // NOI18N
        mrpTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        mrpTF.setText(resourceMap.getString("mrpTF.text")); // NOI18N
        mrpTF.setName("mrpTF"); // NOI18N

        qtyTF.setFont(resourceMap.getFont("qtyTF.font")); // NOI18N
        qtyTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        qtyTF.setText(resourceMap.getString("qtyTF.text")); // NOI18N
        qtyTF.setName("qtyTF"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(colorCB, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(unitCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 119, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(mrpTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(qtyTF))
                    .add(jLabel4))
                .add(18, 18, 18)
                .add(addColorBT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(deleteColorBT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(27, 27, 27))
            .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel3)
                            .add(jLabel14)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(colorCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(unitCB)
                            .add(mrpTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(qtyTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(deleteColorBT, 0, 0, Short.MAX_VALUE)
                        .add(addColorBT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5)
                .addContainerGap())
        );

        jButton1.setAction(actionMap.get("setProductName")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jButton2.setAction(actionMap.get("editProductName")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setPreferredSize(new java.awt.Dimension(57, 33));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "S.NO", "Product Color/ Type", "Product Unit", "M.R.P", "Qty."
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N
        jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N
        jTable1.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable1.columnModel.title3")); // NOI18N
        jTable1.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 777, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(317, 317, 317)
                .add(jButton4)
                .addContainerGap(323, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(62, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLabel13)
                        .add(5, 5, 5)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(layout.createSequentialGroup()
                                .add(jLabel12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(jLabel1))
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(companyNameCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 283, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(productNameCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 283, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(14, 14, 14)
                                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .add(114, 114, 114))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(652, Short.MAX_VALUE)
                .add(jLabel16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel9)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(27, 27, 27)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel2)
                                .add(jLabel13))
                            .add(layout.createSequentialGroup()
                                .add(29, 29, 29)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel1)
                                    .add(jLabel12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(layout.createSequentialGroup()
                                        .add(companyNameCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(6, 6, 6))
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(jButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(6, 6, 6)))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                        .add(productNameCB)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel16)
                            .add(jLabel9))))
                .add(11, 11, 11)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(38, 38, 38)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 362, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 27, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void companyNameCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_companyNameCBActionPerformed
        // TODO add your handling code here:
    this.getProductNameByCompany();
    }//GEN-LAST:event_companyNameCBActionPerformed

    private void productNameCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_productNameCBActionPerformed
        // TODO add your handling code here:
        this.getColorByProduct();
        jLabel10.setForeground(Color.black);
        jLabel10.setText("Add New Products and Edit Existing product.");
         
    }//GEN-LAST:event_productNameCBActionPerformed

    private void colorCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorCBActionPerformed
        // TODO add your handling code here:
       this.getUnitByColor();
       if(colorCB.getEditor().getItem().toString().equalsIgnoreCase("") == false && colorCB.getEditor().getItem().toString().trim().equalsIgnoreCase("<SELECT COLOR>")== false ){
           addColorBT.setEnabled(true);
           deleteColorBT.setEnabled(true);
       }
       else{
           addColorBT.setEnabled(false);
           deleteColorBT.setEnabled(false);
       }
       jLabel10.setForeground(Color.black);
       jLabel10.setText("Add New Products and Edit Existing product.");
    }//GEN-LAST:event_colorCBActionPerformed

    private void addColorBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addColorBTActionPerformed
        // TODO add your handling code here:
        try{
        String productCode  = SVPPUEntityManager.createNativeQuery("SELECT product_code FROM product_tbl where product_name = '" + productNameCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
//        List<String> colorList = SVPPUEntityManager.createNativeQuery("SELECT DISTINCT(product_color) FROM product_unit_tbl where product_code ='" + productCode+"'").getResultList();
//        String productId  = SVPPUEntityManager.createNativeQuery("SELECT iproduct_unitID FROM product_unit_tbl where product_code = '" + productCode+"' AND product_color = '"+ colorCB.getEditor().getItem().toString().trim()+"'"
//                + "AND product_unit = '" + unitCB.getEditor().getItem().toString().trim() +"' AND product_unitName = '"+unitNameCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
//        Vector<String> colorDetails = null;
//        colorDetails.add(colorCB.getEditor().getItem().toString());
//        colorDetails.add(unitCB.getEditor().getItem().toString());
//        colorDetails.add(unitNameCB.getEditor().getItem().toString());
//        colorDetails.add(mrpTF.getText().trim());
//        colorDetails.add(qtyTF.getText().trim());
//        JTable t = new JTable();
//       DefaultTableModel model = (DefaultTableModel) t.getModel() ;
//       model.addRow(colorDetails);
       ProductUnitTblJpaController p = new ProductUnitTblJpaController(SVPPUEntityManager.getEntityManagerFactory());
       ProductColorTblJpaController pColor = new ProductColorTblJpaController(SVPPUEntityManager.getEntityManagerFactory());
       ProductUnitTbl p1 = new ProductUnitTbl();
       ProductTbl productCode1 = new ProductTbl();
       ProductColorTbl productColor = new ProductColorTbl();
       try{
                 if(pColor.findProductColorTbl(colorCB.getEditor().getItem().toString().trim()).getProductColor().equalsIgnoreCase(colorCB.getEditor().getItem().toString().trim())){
//                        productColor.setProductColor(colorCB.getEditor().getItem().toString().trim()); 
//                        productColor.setProductCode(String.valueOf(pColor.getProductColorTblCount() + 1));
//                        pColor.create(productColor);
                     System.out.println("product Already available");
                   }       
       }
       catch(Exception e){
           productColor.setProductColor(colorCB.getEditor().getItem().toString().trim()); 
          productColor.setProductCode(String.valueOf(pColor.getProductColorTblCount() + 1));
          pColor.create(productColor);
       }
       p1.setIproductunitID(p.getProductUnitTblCount()+1);
       productCode1.setProductCode(productCode);
       productColor.setProductColor(colorCB.getEditor().getItem().toString());
       p1.setProductCode(productCode1);
       p1.setProductColor(productColor);
       p1.setProductUnit(unitCB.getEditor().getItem().toString());
      p1.setProductunitName("");
       p1.setProductMrp(Float.parseFloat(mrpTF.getText().trim()));
       p1.setProductQty(Float.parseFloat(qtyTF.getText().trim()));
       if(colorCB.getEditor().getItem().toString().trim().equalsIgnoreCase("") == false && colorCB.getEditor().getItem().toString().trim().equalsIgnoreCase("<SELECT COLOR>") == false && unitCB.getEditor().getItem().toString().trim().equalsIgnoreCase("<SELECT UNIT>")== false
               && unitCB.getEditor().getItem().toString().trim().equalsIgnoreCase("") == false){
          try{
           String productColorUnitID  = SVPPUEntityManager.createNativeQuery("SELECT iproduct_unitID FROM product_unit_tbl where product_code = '" + productCode+"' AND product_color = '"+productColor.getProductColor()+"' AND product_unit = '" +unitCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
           System.out.println("unit - " + productColorUnitID );
           p1.setIproductunitID(Integer.parseInt(productColorUnitID));
                p.edit(p1);
                List<String> colorList = SVPPUEntityManager.createNativeQuery("SELECT DISTINCT(product_color) FROM product_unit_tbl where product_code ='" + productCode+"'").getResultList();
               colorCB.setModel(new javax.swing.DefaultComboBoxModel (colorList.toArray()));
               JTextField colorTF = (JTextField)colorCB.getEditor().getEditorComponent();
               colorTF.setText(""); 
               unitCB.setModel(new javax.swing.DefaultComboBoxModel ());
               JTextField unitTF = (JTextField) unitCB.getEditor().getEditorComponent();
               unitTF.setText(""); 
               mrpTF.setText("0.00");
               qtyTF.setText("0.0");
               addColorBT.setEnabled(false);
               deleteColorBT.setEnabled(false);
          }
          catch(ArrayIndexOutOfBoundsException ae){
               p.create(p1);
               List<String> colorList = SVPPUEntityManager.createNativeQuery("SELECT DISTINCT(product_color) FROM product_unit_tbl where product_code ='" + productCode+"'").getResultList();
               colorCB.setModel(new javax.swing.DefaultComboBoxModel (colorList.toArray()));
               JTextField colorTF = (JTextField)colorCB.getEditor().getEditorComponent();
               colorTF.setText(""); 
               unitCB.setModel(new javax.swing.DefaultComboBoxModel ());
               JTextField unitTF = (JTextField) unitCB.getEditor().getEditorComponent();
               unitTF.setText(""); 
               mrpTF.setText("0.00");
               qtyTF.setText("0.0");
               addColorBT.setEnabled(false);
               deleteColorBT.setEnabled(false);
          }
         
           
           
       }
       else{
            jLabel10.setForeground(Color.red);
           jLabel10.setText("Please select or input Product Unit");
       }
       }catch(Exception e){
           e.printStackTrace();
       }
    }//GEN-LAST:event_addColorBTActionPerformed

    private void colorCBKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_colorCBKeyPressed
        // TODO add your handling code here:
        if(colorCB.getEditor().getItem().toString().equalsIgnoreCase("") == false){
           
          addColorBT.setEnabled(true);
           deleteColorBT.setEnabled(true);
       }
       else{
           addColorBT.setEnabled(false);
           deleteColorBT.setEnabled(false);
       }
    }//GEN-LAST:event_colorCBKeyPressed

    private void unitCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unitCBActionPerformed
        // TODO add your handling code here:
     this.getMrpByUnit();
     jLabel10.setForeground(Color.black);
     jLabel10.setText("Add New Products and Edit Existing product.");
    }//GEN-LAST:event_unitCBActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
     if(companyEntryForm == null){
            JFrame mainFrame = SVPApp.getApplication().getMainFrame();
            companyEntryForm = new CompanyEntryForm(mainFrame, true);
            companyEntryForm .setLocationRelativeTo(mainFrame);
        }

          SVPApp.getApplication().show(companyEntryForm);
        
    }//GEN-LAST:event_jButton5ActionPerformed

    private void colorCBFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_colorCBFocusGained
        // TODO add your handling code here:
        colorCB.setEditable(true);
       
    }//GEN-LAST:event_colorCBFocusGained

    private void colorCBFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_colorCBFocusLost
        // TODO add your handling code here:
         
    }//GEN-LAST:event_colorCBFocusLost
     
    
    
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ProductEntryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ProductEntryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ProductEntryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ProductEntryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                ProductEntryForm dialog = new ProductEntryForm(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    @Action
    public void getProductNameByCompany() {
       //ProductTbl p = new ProductTbl();
        //p.setProductCompanyName(companyNameCB.getEditor().getItem().toString().trim());
        //System.out.println("company name = " + p.getProductCompanyName());
        List<String> productNameList = SVPPUEntityManager.createNativeQuery("select product_name from product_tbl p Left Join  company_tbl c ON  p.`product_companyID`= c.company_code Where c.company_name ='" + companyNameCB.getEditor().getItem().toString().trim()+"'").getResultList();
//        productNameList.add("dinesh");
        Collections.sort(productNameList);
//        for(String l: productNameList){
//        System.out.println(l);
//    }
        productNameCB.setModel(new javax.swing.DefaultComboBoxModel (productNameList.toArray()));
      // productNameCB.addItem("Dinesh");
        new S17NonStrict(productNameCB);
        if(companyNameCB.getEditor().getItem().toString().equalsIgnoreCase("<SELECT COMPANY>") == false && companyNameCB.getEditor().getItem().toString().equalsIgnoreCase("") == false){
        JTextField productNameTF = (JTextField)productNameCB.getEditor().getEditorComponent();
        productNameTF.setText("<SELECT/TYPE A PRODUCT NAME>"); 
        }
        else{
          companyNameCB.getEditor().setItem("<SELECT COMPANY>");  
        }
       if(productNameCB.getItemCount()== 0 ){
         //productNameCB.setModel(new javax.swing.DefaultComboBoxModel ());
           //JTextField productNameTF = (JTextField)productNameCB.getEditor().getEditorComponent();
          // productNameTF.setText(""); 
           colorCB.setModel(new javax.swing.DefaultComboBoxModel ());
           JTextField colorTF = (JTextField)colorCB.getEditor().getEditorComponent();
           colorTF.setText(""); 
           unitCB.setModel(new javax.swing.DefaultComboBoxModel ());
           JTextField unitTF = (JTextField) unitCB.getEditor().getEditorComponent();
           unitTF.setText(""); 
        
         mrpTF.setText("0.00");
         qtyTF.setText("0.0");
       }
       }
    
     @Action
    public void getColorByProduct() {
         try{   
       String productCode =null;
       productCode  = SVPPUEntityManager.createNativeQuery("SELECT product_code FROM product_tbl where product_name = '" + productNameCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
       List<String> colorList = SVPPUEntityManager.createNativeQuery("SELECT DISTINCT(product_color) FROM product_unit_tbl where product_code ='" + productCode+"'").getResultList();
       colorCB.setModel(new javax.swing.DefaultComboBoxModel (colorList.toArray()));
       new S17NonStrict(colorCB);
       JTextField colorTF = (JTextField)colorCB.getEditor().getEditorComponent();
       colorTF.setText("<SELECT COLOR>");  
       colorTF.addFocusListener(this);
       
         }catch(Exception e){
         colorCB.setModel(new javax.swing.DefaultComboBoxModel ());
         JTextField colorTF = (JTextField)colorCB.getEditor().getEditorComponent();
         colorTF.setText(""); 
         unitCB.setModel(new javax.swing.DefaultComboBoxModel ());
         JTextField unitTF = (JTextField) unitCB.getEditor().getEditorComponent();
         unitTF.setText(""); 
         mrpTF.setText("0.00");
         qtyTF.setText("0.0");
        }
         
    }
     
    @Action
    public void getUnitByColor() {
         try{   
       String productCode  = SVPPUEntityManager.createNativeQuery("SELECT product_code FROM product_tbl where product_name = '" + productNameCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
       List<String> unitAndUnitNameList = (Vector) SVPPUEntityManager.createNativeQuery("SELECT DISTINCT (product_unit) FROM product_unit_tbl where product_code = '"+productCode+"' AND product_color ='" + colorCB.getEditor().getItem().toString().trim() +"'").getResultList();
      unitCB.setModel(new javax.swing.DefaultComboBoxModel (unitAndUnitNameList.toArray()));
      JTextField unitTF = (JTextField) unitCB.getEditor().getEditorComponent();
         unitTF.setText("<SELECT UNIT>"); 
         if (colorCB.getEditor().getItem().toString().equalsIgnoreCase("")==false && colorCB.getEditor().getItem().toString().equalsIgnoreCase("<SELECT COLOR>")==false) {
         unitTF.addFocusListener(this);
         }
         
       }catch(Exception e){
         e.printStackTrace();
           unitCB.setModel(new javax.swing.DefaultComboBoxModel ());
         JTextField unitTF = (JTextField) unitCB.getEditor().getEditorComponent();
         unitTF.setText(""); 
         mrpTF.setText("0.00");
         qtyTF.setText("0.0");
        }
    } 
    
     @Action
    public void getMrpByUnit() {
         try{   
       String productCode  = SVPPUEntityManager.createNativeQuery("SELECT product_code FROM product_tbl where product_name = '" + productNameCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
       String productMRP = SVPPUEntityManager.createNativeQuery("SELECT product_mrp FROM product_unit_tbl where product_code = '" + productCode+"' AND product_color = '" +colorCB.getEditor().getItem().toString().trim()+"' AND product_unit = '"+ unitCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
       String productQTY = SVPPUEntityManager.createNativeQuery("SELECT product_qty FROM product_unit_tbl where product_code = '" + productCode+"' AND product_color = '" +colorCB.getEditor().getItem().toString().trim()+"' AND product_unit = '"+ unitCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
       mrpTF.setText(productMRP);
       qtyTF.setText(productQTY);
       }catch(Exception e){
         e.printStackTrace();
         mrpTF.setText("0.00");
         qtyTF.setText("0.0");
        }
    } 

    @Action
    public void showOptionPane() {
    ProductEntryForm pef = new ProductEntryForm(SVPApp.getApplication().getMainFrame(), true);
   if(companyNameCB.getEditor().getItem().toString().equalsIgnoreCase("")==false){
   int newType =  JOptionPane.showConfirmDialog(rootPane, "Do you want to close Product Details Entry?", "Warning", JOptionPane.YES_NO_OPTION);
   if (newType == JOptionPane.YES_OPTION){
      this.dispose();
      pef.dispose(); 
   }
   }
   else{
       this.dispose();
            pef.dispose(); 
   }
    }
    
    public void setEditingflag(boolean flag){
        editingFlag = flag;
    }
    public boolean getEditingFlag(){
        return editingFlag;
    }

   @Action
   public void editProductName(){
        String productCode = null;
        boolean prodInList = false;
        
        if (jTextField1.isShowing()==false){
           if(productNameCB.getEditor().getItem().toString().equalsIgnoreCase("") == false && productNameCB.getEditor().getItem().toString().equalsIgnoreCase("<SELECT/TYPE A PRODUCT NAME>") == false){
               
               List<String> productList = SVPPUEntityManager.createNativeQuery("SELECT product_name FROM product_tbl WHERE product_company_name='"+companyNameCB.getEditor().getItem().toString().trim()+"'").getResultList();  
               
                for (String productName :productList){
                    if(productNameCB.getEditor().getItem().toString().trim().equalsIgnoreCase(productName)==true){      
                        prodInList = true;
                        break;
                    }
                }
                if(prodInList == true)    
                {          
                productNameCB.setVisible(false);
                jTextField1.setBounds(productNameCB.getBounds());
                jTextField1.setFocusable(true);
                jTextField1.setBackground(Color.YELLOW);
                jTextField1.setFont(productNameCB.getFont());
                jTextField1.setText(productNameCB.getEditor().getItem().toString());
                this.add(jTextField1); 
                jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/svp/resources/TickButton.png")));
                companyNameCB.setEnabled(false);
                jButton5.setEnabled(false);
                jButton1.setEnabled(false);
                jButton3.setEnabled(false);
                colorCB.setEnabled(false);
                unitCB.setEnabled(false);
                mrpTF.setEnabled(false);
                qtyTF.setEnabled(false);
                addColorBT.setEnabled(false);
                deleteColorBT.setEnabled(false);
                } 
            }
        } else{
            prodInList = false;
            for (int i =0;i<productNameCB.getItemCount();i++){
                        String productName = productNameCB.getItemAt(i).toString();
                        if(jTextField1.getText().trim().equalsIgnoreCase(productName)==true){      
                        prodInList = true;
                        break;
                        }
             }
            productCode = SVPPUEntityManager.createNativeQuery("SELECT product_code FROM product_tbl WHERE product_name = '"+ productNameCB.getEditor().getItem().toString().trim()+"'").getResultList().get(0).toString();
              if((productNameCB.getEditor().getItem().toString().equalsIgnoreCase(jTextField1.getText())==false) && prodInList ==false){
                        ProductTblJpaController pControl = new ProductTblJpaController(SVPPUEntityManager.getEntityManagerFactory());
                        ProductTbl prdTbl = pControl.findProductTbl(productCode);
                        prdTbl.setProductName(jTextField1.getText());
                        try {
                            pControl.edit(prdTbl);
                            jLabel10.setForeground(Color.blue);
                            jLabel10.setText("Product edited successfully!");
                            this.remove(jTextField1);
                            this.getProductNameByCompany();
                            productNameCB.setVisible(true);
                            jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/svp/resources/Editing.png")));
                            companyNameCB.setEnabled(true);
                            jButton5.setEnabled(true);
                            jButton1.setEnabled(true);
                            jButton3.setEnabled(true);
                            colorCB.setEnabled(true);
                            unitCB.setEnabled(true);
                            mrpTF.setEnabled(true);
                            qtyTF.setEnabled(true);
                        } catch (IllegalOrphanException ex) {
                            Logger.getLogger(ProductEntryForm.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NonexistentEntityException ex) {
                            Logger.getLogger(ProductEntryForm.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (Exception ex) {
                            Logger.getLogger(ProductEntryForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                       
                }else{
                    this.remove(jTextField1);
                    this.getProductNameByCompany();
                    productNameCB.setVisible(true);
                    jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/svp/resources/Editing.png")));
                    companyNameCB.setEnabled(true);
                    jButton5.setEnabled(true);
                    jButton1.setEnabled(true);
                    jButton3.setEnabled(true);
                    colorCB.setEnabled(true);
                    unitCB.setEnabled(true);
                    mrpTF.setEnabled(true);
                    qtyTF.setEnabled(true);
                }        
         
        }
   }
    
    @Action
    public void setProductName() {
        
        boolean productInList =  false;
        // checking if the product name is already exist in database
       if(productNameCB.getEditor().getItem().toString().equalsIgnoreCase("<SELECT/TYPE A PRODUCT NAME>") == false && productNameCB.getEditor().getItem().toString().equalsIgnoreCase("") == false){
          List<String> productList = SVPPUEntityManager.createNativeQuery("SELECT product_name FROM product_tbl WHERE product_company_name = '"+ companyNameCB.getEditor().getItem().toString().trim()+"'").getResultList(); 
          if(productList.isEmpty()){
              productInList = true;
          }
          else{
          for(String productName : productList){
              if(productNameCB.getEditor().getItem().toString().trim().equalsIgnoreCase(productName) == true){
                   productInList = true;
                   jLabel10.setForeground(Color.red);
                   jLabel10.setText("Product already exist. Please type a new product name.");
                   break;
              }
          }
          }
          // if the product not in database adding it to the database
          if(productInList==false){
               String productCodeLast = null;
             try{
               productCodeLast = SVPPUEntityManager.createNativeQuery("SELECT CAST(product_code As UNSIGNED) as productCode FROM product_tbl ORDER BY productCode DESC").getResultList().get(0).toString(); 
             }catch(Exception e){
                 productCodeLast = "0";
             }
              Integer lastProductCode = Integer.parseInt(productCodeLast) + 1;             
              String CompanyCode = SVPPUEntityManager.createNativeQuery("SELECT company_code FROM company_tbl WHERE company_name = '"+ companyNameCB.getEditor().getItem().toString() +"'" ).getResultList().get(0).toString();
             //inserting product information into database.
              ProductTbl p = new ProductTbl();
              ProductTblJpaController p1 = new ProductTblJpaController(SVPPUEntityManager.getEntityManagerFactory());
              
              p.setProductCode(lastProductCode.toString());
              p.setProductName(productNameCB.getEditor().getItem().toString().trim());
              p.setProductcompanyID(CompanyCode);
              //p.setProductCompanyName(companyNameCB.getEditor().getItem().toString().trim()); //SChema Change
                         
                try {
                    
                    p1.create(p);
                    jLabel10.setForeground(Color.blue);
                   jLabel10.setText("Product successfully added!");
                    
                } catch (PreexistingEntityException ex) {
                    Logger.getLogger(ProductEntryForm.class.getName()).log(Level.SEVERE, null, ex);
                    ex.printStackTrace();
                } catch (Exception ex) {
                    Logger.getLogger(ProductEntryForm.class.getName()).log(Level.SEVERE, null, ex);
                    ex.printStackTrace();
                }
               finally{
                    //List<String> productListUpdated = SVPPUEntityManager.createNativeQuery("SELECT product_Name FROM product_tbl").getResultList();
                    //Collections.sort(productListUpdated);
                    //productNameCB.setModel(new javax.swing.DefaultComboBoxModel (productListUpdated.toArray()));
               this.getProductNameByCompany();
               JTextField productTF = (JTextField)productNameCB.getEditor().getEditorComponent();
               productTF.setText("<SELECT/TYPE A PRODUCT NAME>"); 
                
                }
              
          }
       
       } 
        
    }
@Action
public void deleteProductColor(){
    
    String productCode =  SVPPUEntityManager.createNativeQuery("SELECT product_code FROM product_tbl WHERE product_name = '"+ productNameCB.getEditor().getItem().toString().trim()+"';" ).getResultList().get(0).toString();  
    String productUnitCode =  SVPPUEntityManager.createNativeQuery("SELECT iproduct_unitID FROM product_unit_tbl WHERE product_code ='"+productCode+"' AND product_color ='"+ colorCB.getEditor().getItem().toString().trim()+"'  AND product_unit = '"+ unitCB.getEditor().getItem().toString().trim()+"';" ).getResultList().get(0).toString();  
    ProductUnitTblJpaController pu = new ProductUnitTblJpaController(SVPPUEntityManager.getEntityManagerFactory());
        try {
            //ProductUnitTbl puTbl = pu.findProductUnitTbl(Integer.parseInt(productCode));
            pu.destroy(Integer.parseInt(productUnitCode));
            this.getColorByProduct();
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ProductEntryForm.class.getName()).log(Level.SEVERE, null, ex);
        }
}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.persistence.EntityManager SVPPUEntityManager;
    private javax.swing.JButton addColorBT;
    private javax.swing.JComboBox colorCB;
    private javax.swing.JComboBox companyNameCB;
    private javax.swing.JButton deleteColorBT;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField mrpTF;
    private javax.swing.JComboBox productNameCB;
    private javax.swing.JTextField qtyTF;
    private javax.swing.JComboBox unitCB;
    // End of variables declaration//GEN-END:variables
    public boolean editingFlag = false;
    TextField jTextField1 = new  TextField();
    CompanyEntryForm companyEntryForm;

    public void focusGained(FocusEvent e) {
        if(colorCB.getEditor().getItem().toString().equalsIgnoreCase("<SELECT COLOR>")){
        colorCB.getEditor().setItem("");
        colorCB.showPopup();
        
        }
        if (unitCB.getEditor().getItem().toString().equalsIgnoreCase("<SELECT UNIT>")) {
            unitCB.getEditor().setItem("");
            unitCB.showPopup();
        }
        if (colorCB.getEditor().getItem().toString().equalsIgnoreCase("")) {
          
         unitCB.getEditor().setItem("<SELECT UNIT>");
         mrpTF.setText("0.00");
         qtyTF.setText("0.0");
         }
    }

    public void focusLost(FocusEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
